package com.cynteka.cyfoman.core;

import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.util.List;

/**
 * User: Sergey
 * Date: 06.08.11
 * Time: 14:49
 */
public class DBInitTask implements Task {
    private boolean active = true;
    private DataSource dataSource;
    private List<String> scripts;

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public List<String> getScripts() {
        return scripts;
    }

    public void setScripts(List<String> scripts) {
        this.scripts = scripts;
    }

    @Override
    public boolean isActive() {
        return active;
    }

    @Override
    public void run() {
        JdbcTemplate template = new JdbcTemplate(dataSource);
        for(String script : scripts) {
            try {
            template.execute(script);
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
        active = false;
    }
}
